
package com.idsmanager.zdh.service.business.portal;


import com.idsmanager.commons.utils.PasswordHandler;
import com.idsmanager.commons.web.context.BeanProvider;
import com.idsmanager.oauth.domain.security.SecurityUtils;
import com.idsmanager.oauth.domain.security.User;
import com.idsmanager.oauth.domain.security.UserRepository;
import com.idsmanager.oauth.service.dto.user.UserTableDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateUserTableHandler {


    private static final Logger LOG = LoggerFactory.getLogger(CreateUserTableHandler.class);

    private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);

    private UserTableDto userTableDto;

    public CreateUserTableHandler(UserTableDto userTableDto) {
        this.userTableDto = userTableDto;
    }

    public String handle() {
        final String username = userTableDto.getUsername();
        final String pass = PasswordHandler.encryptPassword(userTableDto.getPassword(), username);
        User user = new User(username, pass);
        user.privileges().addAll(userTableDto.getPrivileges());


        userRepository.saveUser(user);
        LOG.debug("{}|Create User: {}", SecurityUtils.username(), user);

        return user.uuid();
    }
}
